//引入模块
const express = require('express');
const mysql = require('../mysql');

//创建路由对象
const router = express.Router();

//用户注册
router.post('/reg', (req, res) => {
    //获取post的数据
    let body = req.body;
    //验证各项数据是否为空
    let i = 400;
    for (let key in body) {
        i++;
        //console.log(body[key]);
        if (!body[key]) {
            return res.send({ code: i, msg: `${key} null` });
        }
    }

    //查询用户名是否重复
    mysql.query('select * from xz_user where uname=?', [body.uname], (err, result) => {
        //用户名不重复则插入数据
        if (result.length == 0) {
            mysql.query('insert into xz_user set ?', [body], (err, result) => {
                if (err) {
                    return res.send({ code: 301, msg: "false" });
                }
                return res.send({ code: 200, msg: "success" });
            })
        } else {
            res.send({ code: 201, msg: "name respect" });
        }
    })
});

//用户登录
router.post('/login', (req, res) => {
    //保存解析后的数据
    let body = req.body;
    //判断数据是否为空
    let i = 400;
    for (let key in body) {
        i++;
        //console.log(body[key]);
        if (!body[key]) {
            return res.send({ code: i, msg: `${key} null` });
        }
    }

    //数据库查询
    mysql.query('select * from xz_user where uname=? and upwd=?', [body.uname, body.upwd], (err, result) => {
        if (err) {
            return res.send({ code: 400, msg: "login error" });
        }
        //返回值为空时证明不匹配
        if (result.length == 0) {
            return res.send({ code: 301, msg: "uname false or password false" });
        }
        res.send({ code: 200, msg: "success" });
    });
});

//用户检测
router.get('/checkUser', (req, res) => {
    //获取解析数据
    let body = req.query;
    if (!body.uname) {
        return res.send({ code: 401, msg: "name null" });
    }

    //根据用户名进行查询是否存在
    mysql.query('select * from xz_user where uname=?', [body.uname], (err, result) => {
        if (err) {
            return res.send({ code: 400, msg: "error" });
        }
        //存在则返回
        if (result.length) {
            return res.send({ code: 201, msg: "exists" });
        }
        res.send({ code: 200, msg: "not exists" });
    });
});

//用户修改
router.post('/update', (req, res) => {
    //解析数据
    let body = req.body;
    //console.log(body);

    //遍历验证是否为空
    let i = 400;
    for (let key in body) {
        i++;
        //console.log(body[key]);
        if (!body[key]) {
            return res.send({ code: i, msg: `${key} null` });
        }
    }
    //将性别转为数字
    body.gender = body.gender == '男' ? 1 : 0;

    //进行sql更新
    mysql.query('update xz_user set ? where uid=?', [body, body.uid], (err, result) => {
        if (err) throw err;
        if (result.affectedRows == 0) {
            return res.send({ code: 301, msg: "update false" });
        }
        console.log(result);
        res.send({ code: 200, msg: "update success" });
    });
});

//用户分页查询
router.get('/list', (req, res) => {
    //解析数据，默认值
    let body = req.query;
    // body.page = body.page == '' ? 1 : Number(body.page);
    // body.size = body.size == '' ? 4 : Number(body.size);
    if(!body.page) body.page = 1;
    if(!body.size) body.size = 4;

    let page = (body.page - 1) * body.size;
    let size = parseInt(body.size);

    //执行sql命令
    mysql.query('select * from xz_user limit ?,?', [page, size], (err, result) => {
        if (err) throw err;
        res.send(result);
    });
});

//用户删除
router.get('/delete',(req,res)=>{
    let body = req.query;
    if(!body.uid){
        return res.send({code:401,msg:"uid null"});
    }

    //执行sql
    mysql.query('delete from xz_user where uid=?',body.uid,(err,result)=>{
        if(err) throw err;
        //console.log(result);
        if(result.affectedRows == 0){
            return res.send({code:301,msg:"delete error"});
        }
        res.send({code:200,msg:"delete success"});
    });
});

module.exports = router;